package inventory.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import inventory.entity.Equipment;
import inventory.entity.MaterialStuff;
import inventory.entity.SaleRequest;
import inventory.entity.Terminal;
import inventory.entity.po.SaleRequestPo;
import inventory.mappers.*;
import inventory.service.SaleRequestService;
import inventory.utils.UUIDUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author sxt
 * @since 2019-11-06
 */
@Service
public class SaleRequestServiceImpl extends ServiceImpl<SaleRequestMapper, SaleRequest> implements SaleRequestService {


    @Resource
    private TerminalMapper terminalMapper;

    @Resource
    private EquipmentMapper equipmentMapper;

    @Resource
    private MaterialStuffMapper materialStuffMapper;

    @Resource
    private SaleRequestMapper saleRequestMapper;

    @Override
    public List<SaleRequest> addSaleRequest(List<SaleRequest> saleRequests) {

        ArrayList<SaleRequest> saleRequestRs = new ArrayList<>();


        for (SaleRequest s : saleRequests) {
            Integer type = s.getSaleType();
            if (type == 1) {//终端销库请求处理
                Terminal terminal = new Terminal();
                terminal.setImei(s.getSerialNum());
                terminal.setTerminalTypicalId(s.getTypicalId());
                terminal.setRepositoryId(s.getRepositoryId());
                Terminal rs = terminalMapper.selectOne(terminal);//检查对应记录是否存在仓库中

                if (rs == null) {
                    saleRequestRs.add(s);//将无效销库数据记录
                    continue;
                }

                List<SaleRequest> hasSaleRequests = saleRequestMapper.selectList(new EntityWrapper<SaleRequest>()
                        .eq("serial_num", s.getSerialNum())
                        .eq("repository_id", s.getRepositoryId()));//检查该物料是否已存在销库请求
                if (hasSaleRequests.size() > 0) {
                    saleRequestRs.add(s);//将无效销库数据记录
                    continue;
                }

                s.setRequestTime(new Date());
                s.setStatus(0);//初始化请求状态为：0未处理
                s.setSaleId(UUIDUtils.getId());
                s.insert();
            } else if (type == 2) {//设备销库请求处理
                Equipment equipment = new Equipment();
                equipment.setImei(s.getSerialNum());
                equipment.setMaterialTypicalId(s.getTypicalId());
                equipment.setRepositoryId(s.getRepositoryId());
                Equipment rs = equipmentMapper.selectOne(equipment);//检查对应记录是否存在仓库中

                if (rs == null) {
                    saleRequestRs.add(s);//将无效销库数据记录
                    continue;
                }

                List<SaleRequest> hasSaleRequests = saleRequestMapper.selectList(new EntityWrapper<SaleRequest>()
                        .eq("serial_num", s.getSerialNum())
                        .eq("repository_id", s.getRepositoryId()));//检查该物料是否已存在销库请求
                if (hasSaleRequests.size() > 0) {
                    saleRequestRs.add(s);//将无效销库数据记录
                    continue;
                }

                s.setRequestTime(new Date());
                s.setStatus(0);//初始化请求状态为：0未处理
                s.setSaleId(UUIDUtils.getId());
                s.insert();
            } else if (type == 3) {//物料销库请求处理

                MaterialStuff materialStuff = new MaterialStuff();
                materialStuff.setStuffId(s.getStuff_id());
                materialStuff.setStuffTypicalId(s.getTypicalId());
                materialStuff.setRepositoryId(s.getRepositoryId());
                MaterialStuff rs = materialStuffMapper.selectOne(materialStuff);

                if (rs == null) {
                    saleRequestRs.add(s);//将无效销库数据记录
                    continue;
                }

                List<SaleRequest> hasSaleRequests = saleRequestMapper.selectList(new EntityWrapper<SaleRequest>()
                        .eq("stuff_id", s.getStuff_id())
                        .eq("repository_id", s.getRepositoryId()));//检查该物料是否已存在销库请求
                if (hasSaleRequests.size() > 0) {
                    SaleRequest saleRequest = hasSaleRequests.get(0);//追加已有数据记录
                    saleRequest.setNum(s.getNum());
                    long left = rs.getInRepository() - saleRequest.getNum();
                    if (left > 0) {
                        saleRequest.insertOrUpdate();
                    } else {
                        saleRequestRs.add(s);
                    }
                    continue;
                }

                s.setRequestTime(new Date());
                s.setStatus(0);//初始化请求状态为：0未处理
                s.setSaleId(UUIDUtils.getId());
                s.insert();
            }

        }

        return saleRequestRs;
    }

    @Override
    public List<SaleRequest> querySaleRequestsByCondition(Map<String, Object> columnMap) {
        List<SaleRequest> saleRequests = saleRequestMapper.selectByMap(columnMap);
        return saleRequests;
    }

    @Autowired
    private SaleRequestPoMapper saleRequestPoMapper;

    @Override
    public List<SaleRequestPo> querySaleRequestByUserId(String userId) {
        return saleRequestPoMapper.selectList(new EntityWrapper<SaleRequestPo>().eq("user_id", userId));
    }

    @Override
    public List<SaleRequestPo> querySaleRequestByRoleId(String roleId) {
        return saleRequestPoMapper.selectList(new EntityWrapper<SaleRequestPo>().eq("role_id", roleId));
    }


}
